/**
 * Used to instantiate queen pieces that are used in the chess game. Queen extends Piece and inherits the instance
 * variables that are common to all the pieces in the chess game
 * 
 * @author jmikhail
 * @version 1.13
 */

package pieces;
import mainClasses.*;

public class Queen extends Piece{
	
	/**
	 * Set to true if a piece is blocking the movement of the queen on the board
	 */
	public boolean blockage = false;
	
	
	
	
	
	
	/**
	 * Constructor for the class Queen. It creates a new piece with the specified color at the 
	 * coordinates given and places it on board b
	 * 
	 * @param xaxis The position of the piece on the board in the direction of the X axis
	 * @param yaxis The position of the piece on the board in the direction of the Y axis
	 * @param b Board on which the queen is used
	 * @param color Color or side of the piece
	 */
	public Queen(int xaxis, int yaxis, Board board, String color)
	{
		super(xaxis, yaxis, board, "  Queen  ", color);
		if(color!="White")
		{
			super.type = "BK->Queen";
			
		}
	}
	
	
	
	
	
	/**
	 * Checks if the movement to the given point is in the allowed directions of the queen. i.e., vertical, horizontal,
	 * of diagonal. If possible, it checks if there is another piece in the path blocking the movement and if there is
	 * a piece of the same color at the destination. If the movement is allowed, it returns true
	 * 
	 * @param p2 Destination of the movement
	 * @return A boolean that is set to true if the move to the point given is allowed
	 */
	public boolean movePossible(int desX, int desY) {

		int inx = this.x;
		int iny = this.y;
		if(desX>7||desX<0||desY>7||desY<0)
        {
        	return false; 	//Returns false if the input is invalid (out of bounds)
        }else if (Math.abs(inx - desX) > 0 && Math.abs(iny - desY) == 0) 	//Horizontal Movement
		{
			if (inx > desX) {										//Moving to the left
				for (int i = desX; i <= inx; i++) {
					if (i != desX && i != inx && !this.board.getPiece(i, this.y).getType().equals("         "))
					{
						blockage = true;
					}
				}
			} else if (desX > inx) {								//Moving to the right
				for (int i = inx; i <= desX; i++) {
					if (i != desX && i != inx && !this.board.getPiece(i, this.y).getType().equals("         ")) {
						blockage = true;
					}
				}
			}
			if (blockage == false) {								//If there is no blockage, return true
				if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) {
					return true;
				}
			}
			blockage = false;
			return false;											//If there is a blockage, return false
		} 
		else if (Math.abs(inx - desX) == 0 && Math.abs(iny - desY) > 0) {	//Vertical Movement
			if (iny > desY) {												//Moving downwards
				for (int i = desY; i <= iny; i++) {
					if (i != desY && i != iny && !(this.board.getPiece(this.x, i) instanceof NullPiece)) {
						blockage = true;
					}
				}
			} else if (desY > iny) {									//Moving upwards
				for (int i = iny; i <= desY; i++) {
					if (i != desY && i != iny && !(this.board.getPiece(this.x, i) instanceof NullPiece)) {
						blockage = true;
					}
				}
			}
			if (blockage == false) 									//If there is no blockage, return true
			{
				if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) 
				{
					return true;
				}
			}
			blockage = false;
			return false;
		} else if (Math.abs(inx - desX) == Math.abs(iny - desY)) 
        {
            if (super.x < desX && super.y < desY) 
            {
                int x = super.x;
                int y = super.y;
                for (int i = x; i < desX; i++) {
                    x++;
                    y++;
                    Piece temp = super.board.getPiece(x, y);
                    if (!(temp  instanceof NullPiece) && x != super.x && x != desX) 
                    {
                        blockage = true;
                    }
                }
                if (blockage == false) 
                {
                    if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) 
                    {
                        return true;
                    }
                }
                blockage = false;
                return false;
            } else if (super.x > desX && super.y < desY) 
            {
                int x = super.x;
                int y = super.y;
                for (int i = y; i < desY; i++) 
                {
                    y++;
                    x--;
                    Piece temp = super.board.getPiece(x, y);
                    if (!(temp instanceof NullPiece) && y != super.y && y != desY) 
                    {
                        blockage = true;
                    }
                }
                if (blockage == false)
                {
                    if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) 
                    {
                        return true;
                    }
                }
                blockage = false;
                return false;
            } else if (super.x > desX && super.y > desY) 
            {
                int x = super.x;
                int y = super.y;
                for (int i = desY; i < y; i++) 
                {
                    y--;
                    x--;
                    Piece temp = super.board.getPiece(x, y);
                    if (!(temp instanceof NullPiece) && y != super.y && y != desY) 
                    {
                        blockage = true;
                    }
                }
                if (blockage == false) 
                {
                    if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) 
                    {
                        return true;
                    }
                }
                blockage = false;
                return false;
            } else if (super.x < desX && super.y > desY) 
            {
                int x = super.x;
                int y = super.y;
                for (int i = x; i < desX; i++) 
                {
                    y--;
                    x++;
                    Piece temp = super.board.getPiece(x, y);
                    if (!(temp instanceof NullPiece) && x!=super.x && x!=desX) 
                    {
                        blockage = true;
                    }
                }
                if (blockage == false)
                {
                    if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) 
                    {
                        return true;
                    }
                }
                blockage = false;
                return false;
            }
        }
		return false;
	}
	
	
	
	
	
	
	/**
	 * Returns True if a Queen could be moved to the new position given regardless of the other
	 * pieces on the board
	 * 
	 * @param p2 Position of the queen to be placed
	 * @return A boolean set to true if a queen can be placed at p2
	 */
	public boolean eightQueenMove(int desX, int desY)
	{
		if(Math.abs(x-desX)>0 && Math.abs(y-desY)==0)
		{
			return true;
		}else if(Math.abs(x-desX)==0 && Math.abs(y-desY)>0)
		{
			return true;
		}else if(Math.abs(x-desX)==Math.abs(y-desY))
		{
			return true;
		}else
		{
			return false;
		}
	}
}